/*
 * /linux-3.0.8/drivers/spi/nrf2401_test.c
 *
 */

#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <fcntl.h>
#include <time.h>
#include <sys/ioctl.h>
#include <linux/types.h>

#include "nrf2401_test.h"

#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))

//static const char *device = "/dev/nrf2401-0.0";
static const char *device = "/dev/nrf2401-1.0";
static uint8_t mode = SPI_MODE_0;
static uint8_t bits = 8;
static uint32_t speed = 500000;
static uint16_t delay;

static void pabort(const char *s){perror(s);abort();}
static void print_usage(const char *prog)
{
	printf("Usage: %s [-DsbdlHOLC3]\n", prog);
	puts("  -D --device   device to use (default /dev/spidev1.1)\n"
	     "  -s --speed    max speed (Hz)\n"
	     "  -d --delay    delay (usec)\n"
	     "  -b --bpw      bits per word \n"
	     "  -l --loop     loopback\n"
	     "  -H --cpha     clock phase\n"
	     "  -O --cpol     clock polarity\n"
	     "  -L --lsb      least significant bit first\n"
	     "  -C --cs-high  chip select active high\n"
	     "  -3 --3wire    SI/SO signals shared\n");
	exit(1);
}
static void parse_opts(int argc, char *argv[])
{
	while (1) {
		static const struct option lopts[] = {
			{ "device",  1, 0, 'D' },
			{ "speed",   1, 0, 's' },
			{ "delay",   1, 0, 'd' },
			{ "bpw",     1, 0, 'b' },
			{ "loop",    0, 0, 'l' },
			{ "cpha",    0, 0, 'H' },
			{ "cpol",    0, 0, 'O' },
			{ "lsb",     0, 0, 'L' },
			{ "cs-high", 0, 0, 'C' },
			{ "3wire",   0, 0, '3' },
			{ "no-cs",   0, 0, 'N' },
			{ "ready",   0, 0, 'R' },
			{ NULL, 0, 0, 0 },
		};
		int c;
		c = getopt_long(argc, argv, "D:s:d:b:lHOLC3NR", lopts, NULL);
		if (c == -1)
			break;
		switch (c) {
		case 'D':
			device = optarg;
			break;
		case 's':
			speed = atoi(optarg);
			break;
		case 'd':
			delay = atoi(optarg);
			break;
		case 'b':
			bits = atoi(optarg);
			break;
		case 'l':
			mode |= SPI_LOOP;
			break;
		case 'H':
			mode |= SPI_CPHA;
			break;
		case 'O':
			mode |= SPI_CPOL;
			break;
		case 'L':
			mode |= SPI_LSB_FIRST;
			break;
		case 'C':
			mode |= SPI_CS_HIGH;
			break;
		case '3':
			mode |= SPI_3WIRE;
			break;
		case 'N':
			mode |= SPI_NO_CS;
			break;
		case 'R':
			mode |= SPI_READY;
			break;
		default:
			print_usage(argv[0]);
			break;
		}
	}
}


/***** nrf2401 card ops *****/
static int nrf2401_operate_chip(int fd,
				struct nrf2401_ioc_transfer *xfer)
{
	int ret = 0;

	ret = ioctl(fd, SPI_IOC_MESSAGE(1), xfer);
	if (ret == -1)
		printf(" Y@_@Y (ioctl error) Y@_@Y\n");
	return ret;
}


/***** nrf2401 read, write blk ops *****/
static int nrf2401_read_chip(int fd,
				struct nrf2401_ioc_transfer *xfer)
{
	int i =0;

	xfer->ioc_type = NRF2401_IOC_READ_DATA,
	
	printf("/***** nrf2401 read chip test:  start *****/\n");
	memset(xfer->txrx_buf, 0x00, 
		NRF2401_PIPE_LEN);
	if(nrf2401_operate_chip(fd, xfer))
		return 0;
	for(i=0; i<NRF2401_PIPE_LEN; i++){
		if(i%4 == 0) printf("\n");
		printf("0x%02x	", xfer->txrx_buf[i]);
	}
	printf("\n/***** nrf2401 read chip test:  end *****/\n");
}

static int nrf2401_write_chip(int fd,
				struct nrf2401_ioc_transfer *xfer)
{

	int i =0;

	xfer->ioc_type = NRF2401_IOC_WRITE_DATA,
	
	printf("/***** nrf2401 write chip test:  start *****/\n");
	memset(xfer->txrx_buf, (__u8)(rand()%10), 
		NRF2401_PIPE_LEN);
	if(nrf2401_operate_chip(fd, xfer))
		return 0;
	for(i=0; i<NRF2401_PIPE_LEN; i++){
		if(i%4 == 0) printf("\n");
		printf("0x%02x	", xfer->txrx_buf[i]);
		
	}
	printf("\n/***** nrf2401 write chip test:  end *****/\n");
}

static int nrf2401_goto_menu(int fd,
				struct nrf2401_ioc_transfer *xfer)
{
	return 0;
	//}
}
struct {
	void (*function)(int fd, struct nrf2401_ioc_transfer *xfer);
	char *msg;
}ioc_type_menu[] = {
	{ nrf2401_goto_menu,	"nrf2401 goto before menu: \n" } ,
	{ nrf2401_read_chip,	"nrf2401 read chip : \n" } ,
	{ nrf2401_write_chip,	"nrf2401 write chip : \n\n" } ,

	{ 0, 0}						
};
static int nrf2401_menu_card(int fd)
{
	int i =0;
	int select_index =0;
	struct nrf2401_ioc_transfer xfer={
		
	};
	for(i = 0; i<NRF2401_ADDR_LEN; i++){
		xfer.addr_buf[i] = (__u8)0x01;
	}

	for(i = 0; i<NRF2401_PIPE_LEN; i++){
		xfer.txrx_buf[i] = (__u8)0x24;
	}
	
	while(1){
		while(1)
		{
			printf("\nPlease select function: \n");	
			for(i=0; ioc_type_menu[i].function!=0; i++)
				printf("%d : %s", i, ioc_type_menu[i].msg);
			printf("\nPlease input the select number:");	
			scanf("%d", &select_index) ;	
			printf("You select %d function: %s\n", 
				select_index, ioc_type_menu[select_index].msg);	
			//getchar();
			if(0 < select_index && select_index < i)
			{
				(*ioc_type_menu[select_index].function)(fd, &xfer);
				sleep(1);
			}else{
				break;
			}
			//printf("\nPlease enter key to contine:\n");	
			//getchar();
		}	
	}
	printf("Do not goto here\n");	
	return i;
}

int main(int argc, char *argv[])
{
	int ret = 0;
	int fd = -1;
	int i = 0;

	parse_opts(argc, argv);

	fd = open(device, O_RDWR);
	if (fd < 0)
		pabort("can't open device");

	nrf2401_menu_card(fd);
		/*
	printf("SPI interface mode: \n");
	ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
	if (ret == -1)
		pabort("can't set spi mode");
	ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
	if (ret == -1)
		pabort("can't get spi mode");
	ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
	if (ret == -1)
		pabort("can't set bits per word");

	ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
	if (ret == -1)
		pabort("can't get bits per word");
	ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
	if (ret == -1)
		pabort("can't set max speed hz");
	ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
	if (ret == -1)
		pabort("can't get max speed hz");
	printf("spi mode: %d\n", mode);
	printf("bits per word: %d\n", bits);
	printf("max speed: %d Hz (%d KHz)\n\n\n", speed, speed/1000);
	*/

	
	close(fd);

	return ret;
}
